package com.link.base.base.user.service;

import com.link.base.base.user.model.User;
import com.link.base.base.user.dao.mybatis.mapper.UserImpMapper;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.dao.mapper.BasicMapper;
import com.link.core.basic.model.CallParam;
import com.link.core.basic.service.ServiceException;
import com.link.core.basic.service.BasicServiceImpl;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.util.UserUtil;
import com.link.core.cllog.LCLogger;
import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author unknown
 * @date unknown
 * @comments 用户
 */
@Service
public class UserImpServiceImpl extends BasicServiceImpl<User> implements UserImpService {

    //private static final Logger logger = LogManager.getLogger(UserServiceImpl.class);

    @Resource
    protected KeyGenerateService keyGenerateService;
    @Resource
    private UserImpMapper userImpMapper;

    @Override
    public BasicMapper<User> getBasicMapper() {
        return userImpMapper;
    }

    /**
     * 根据用户名查找用户
     *
     * @param username 用户名
     * @return 用户信息
     */
    @Override
    public User queryByUsername(String username) throws ServiceException {
        //忽略大小写,自动转换成大写
        String usernameUpperCase = username.toUpperCase();
        User record = new User();
        record.setUsername(usernameUpperCase);
        try {
            return userImpMapper.queryByUsername(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void runUserCheck(CallParam record) throws ServiceException {
        try {
            CoreUser user = UserUtil.getUser();
            record.setParam1(user.getId());
            userImpMapper.runUserCheck(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    @Transactional(rollbackFor = ServiceException.class)
    public void runNewUserForimp(CallParam record) throws ServiceException {
        try {
            CoreUser user = UserUtil.getUser();
            record.setParam1(user.getId());
            userImpMapper.runUserCheck(record);
            userImpMapper.runNewUserForimp(record);
        } catch (Exception e) {
            LCLogger.withException(e);
            throw new ServiceException(e.getMessage());
        }
    }
}
